/**
 * 
 */
package com.moioo.util.html;

import java.lang.reflect.Method;
import java.util.Iterator;

/**
 * @author 3400423@qq.com
 *
 */
public class OptionUtil {
	public static String toOptionString(Class classz , Iterator<? extends Object> options,String def,String keyMethod,String valueMethod)throws Exception{
		StringBuilder sb = new StringBuilder();
		Method callKey   = classz.getDeclaredMethod(keyMethod, new Class[0]);
		Method callValue = classz.getDeclaredMethod(valueMethod, new Class[0]);
		while(options.hasNext()){
			Object obj = options.next();
			String key   = (String)callKey.invoke(obj, new Object[0]);
			String value = (String)callValue.invoke(obj, new Object[0]);
			if(key == null || value == null)continue;
			sb.append("<option value=\"")
			.append(key)
			.append("\"");
			if(def != null && def.equals(key))
				sb.append(" selected");
			sb.append(">")
			.append(value)
			.append("</option>");
		}
		return sb.toString();
	}
}
